package com.tca.spring.framework.learning.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author zhoua
 * @Date 2021/9/23
 *
 * 使用注解的方式完成aop
 */
//@Component
@Aspect
@Slf4j
public class LogRecordAspect {

	@Pointcut(value = "@annotation(com.tca.spring.framework.learning.aop.annotation.LogRecord)")
	public void pointcut() {}

	@Around(value = "pointcut()")
	public Object around(ProceedingJoinPoint proceedingJoinPoint) {
		try {
			long startTime = System.currentTimeMillis();
			Object result = proceedingJoinPoint.proceed();
			long endTime = System.currentTimeMillis();
			log.info("execute:[{}], in:[{}], out:[{}], time:[{} ms]", proceedingJoinPoint.getSignature(),
					proceedingJoinPoint.getArgs(), result, endTime - startTime);
			return result;
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		return null;
	}
}
